package revenge.jack_cristo.shiyuanbaoyou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import revenge.jack_cristo.shiyuanbaoyou.R;
import revenge.jack_cristo.shiyuanbaoyou.bean.PinpaiBean;
import revenge.jack_cristo.shiyuanbaoyou.bean.Pinpai_productBean;

/**
 * Created by Jack-Cristo on 2016/11/13.
 */

public class Pinpai_Adapter extends BaseAdapter {
    private Context context;
    private List<PinpaiBean.RowsBean> list;
    private LayoutInflater inflater;
    private int second;
    private int minute;
    private int hour;
    private int day;

    public Pinpai_Adapter(Context context, List<PinpaiBean.RowsBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Viewholder viewholder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.pinpai_intm, null);
            viewholder = new Viewholder(view);
            view.setTag(viewholder);
        } else {
            viewholder = (Viewholder) view.getTag();
        }

        DecimalFormat df = new DecimalFormat("###.00");
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timef = format.format(date);
        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();
        cal1.setTime(date);
        Date date1 = null;
        try {
            date1 = format.parse(list.get(i).getEndDateStr());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal2.setTime(date1);
        second = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 1000 % 60);
        minute = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 1000 / 60 % 60);
        hour = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 1000 / 60 / 60 % 24);
        day = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 1000 / 60 / 60 / 24);

        String logourl = "http://www.syby8.com" + list.get(i).getImgUrlSml();
        Glide.with(context).load(logourl).into(viewholder.logo);
        viewholder.name.setText(list.get(i).getName());
        viewholder.count.setText(df.format(list.get(i).getDisCount()) + "折起");
        viewholder.leastda.setText("还有" + day + "天" + hour + "小时" + minute + "分" + second + "秒");
        String s = "{\"product\":" + list.get(i).getProductInfo() + "}";
        Gson gson = new Gson();
        Pinpai_productBean productBean = gson.fromJson(s, new TypeToken<Pinpai_productBean>() {
        }.getType());
        List<Pinpai_productBean.ProductBean> list = productBean.getProduct();
        Glide.with(context).load(list.get(0).getProductImg()).into(viewholder.product1);
        Glide.with(context).load(list.get(1).getProductImg()).into(viewholder.product2);
        Glide.with(context).load(list.get(2).getProductImg()).into(viewholder.product3);

        viewholder.price_product1.setText("￥" + df.format(list.get(0).getNewPrice()) + "/" + df.format(list.get(0).getDisCount()) + "折");
        viewholder.price_product2.setText("￥" + df.format(list.get(1).getNewPrice()) + "/" + df.format(list.get(1).getDisCount()) + "折");
        viewholder.price_product3.setText("￥" + df.format(list.get(2).getNewPrice()) + "/" + df.format(list.get(2).getDisCount()) + "折");

        return view;
    }

    class Viewholder {
        private ImageView logo, product1, product2, product3;
        private TextView name, count, leastda, price_product1, price_product2, price_product3;

        public Viewholder(View view) {
            logo = (ImageView) view.findViewById(R.id.logo_pinpai);
            product1 = (ImageView) view.findViewById(R.id.product1_pinpi);
            product2 = (ImageView) view.findViewById(R.id.product2_pinpai);
            product3 = (ImageView) view.findViewById(R.id.product3_pinpi);

            name = (TextView) view.findViewById(R.id.name_pinpai);
            count = (TextView) view.findViewById(R.id.count_painpai);
            leastda = (TextView) view.findViewById(R.id.shengyu_pinpai);

            price_product1 = (TextView) view.findViewById(R.id.price_product1);
            price_product2 = (TextView) view.findViewById(R.id.price_product2);
            price_product3 = (TextView) view.findViewById(R.id.price_product3);

        }
    }
}
